package com.beichuan_travel.service.impl;

import com.beichuan_travel.enums.ResultCode;
import com.beichuan_travel.enums.Sort;
import com.beichuan_travel.exception.ApiException;
import com.beichuan_travel.interfaces.transform.FoodCovertBasic;
import com.beichuan_travel.mapper.FoodMapper;
import com.beichuan_travel.model.dto.FoodDTO;
import com.beichuan_travel.model.po.FoodPO;
import com.beichuan_travel.model.vo.FoodVO;
import com.beichuan_travel.model.vo.ResultVO;
import com.beichuan_travel.service.FoodService;
import com.beichuan_travel.utils.ArraysUtils;
import com.beichuan_travel.utils.ResponseResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("foodService")
public class FoodServiceImpl implements FoodService {

    @Resource
    FoodMapper foodMapper;

    @Resource
    ResponseResult  responseResult;

    @Resource
    ArraysUtils arraysUtils;


    @Override
    public ResultVO queryByName(String name) {
        FoodPO foodPO = foodMapper.queryByName(name);
        FoodVO foodVO = new FoodVO();
        foodVO.setImgUrl(foodMapper.imgUrl(foodPO.getId()));
        foodVO.setId(foodPO.getId());
        foodVO.setName(foodPO.getName());
        foodVO.setPrice(foodPO.getPrice());
        foodVO.setStar(foodPO.getStar());
        foodVO.setDescription(foodPO.getDescription());

        return foodVO != null ? responseResult.ok(foodVO) : responseResult.empty();
    }

    @Override
    public List<FoodVO> queryByPrice(Integer lower, Integer upper) {
        List<FoodPO> list = foodMapper.queryByPrice(lower, upper);
        if (list == null && list.isEmpty()) {
            return null;
        }
        List<FoodVO> foodVOList = FoodCovertBasic.INSTANCE.POtoCoertVO(list);
        return foodVOList;
    }

    @Override
    public List<FoodVO> queryByStar(Integer star) {
        List<FoodPO> list = foodMapper.queryByStar(star, star + 1);
        if (list == null && list.isEmpty()) {
            return null;
        }
        List<FoodVO> foodVOList = FoodCovertBasic.INSTANCE.POtoCoertVO(list);
        return foodVOList;
    }

    @Override
    public List<FoodVO> queryAllDescByStar(Integer type) {
        List<FoodPO> list = foodMapper.queryAllByStarDesc();
        if (list == null && list.isEmpty()) {
            return null;
        }
        if (type != Sort.ASC.getSort() && type != Sort.DESC.getSort()) {
            throw new ApiException(ResultCode.VALIDATE_FAILED, "请传入数字0或数字1");
        }
        if (type == Sort.ASC.getSort()) {
            arraysUtils.reverse(list);
        }
        List<FoodVO> foodVOList = FoodCovertBasic.INSTANCE.POtoCoertVO(list);
        return foodVOList;
    }

    @Override
    public List<FoodVO> queryAllASCByPrice(Integer type) {
        List<FoodPO> list = foodMapper.queryAllByPriceAsc();
        if (list == null && list.isEmpty()) {
            return null;
        }
        if (type != Sort.ASC.getSort() && type != Sort.DESC.getSort()) {
            throw new ApiException(ResultCode.VALIDATE_FAILED, "请传入数字0或数字1");
        }
        if (type == Sort.DESC.getSort()) {
            arraysUtils.reverse(list);
        }
        List<FoodVO> foodVOList = FoodCovertBasic.INSTANCE.POtoCoertVO(list);
        return foodVOList;
    }

    @Override
    public List<FoodVO> fuzzyQueryName(String name) {
        List<FoodPO> foodPOList = foodMapper.queryByLikeName("%"+name+"%");
        if (foodPOList == null && foodPOList.isEmpty()) {
            return null;
        }
        List<FoodVO> attractionVOList = FoodCovertBasic.INSTANCE.POtoCoertVO(foodPOList);
        return attractionVOList;
    }

    @Override
    public List<FoodVO> queryAllByName(Integer type) {
        List<FoodPO> attractionPOList = foodMapper.queryAllByNameAsc();
        if (attractionPOList == null && attractionPOList.isEmpty()) {
            return null;
        }
        if (type != Sort.ASC.getSort() && type != Sort.DESC.getSort()) {
            throw new ApiException(ResultCode.VALIDATE_FAILED, "请传入数字0或数字1");
        }
        if (type == Sort.DESC.getSort()) {
            arraysUtils.reverse(attractionPOList);
        }
        List<FoodVO> attractionVOList = FoodCovertBasic.INSTANCE.POtoCoertVO(attractionPOList);
        return attractionVOList;
    }

    @Override
    public int addFood(FoodDTO foodDTO) {
        FoodPO foodPO = new FoodPO();
        foodPO.setDescription(foodDTO.getDescription());
        foodPO.setName(foodDTO.getName());
        foodPO.setPrice(foodDTO.getPrice());
        foodPO.setStar(foodDTO.getStar());
        return foodMapper.addFood(foodPO);
    }
}
